/**************************************************
 * 锟较猴拷锟斤拷锟斤拷锟斤拷锟斤拷萍锟斤拷锟斤拷薰锟剿?http://www.morefuntek.com)
 * 模锟斤拷锟斤拷锟? ExportTableToExcel
 * 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
 * 锟侥碱拷锟斤拷ExportTableToExcel.java
 **************************************************
 */
package com.ywh.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExportToExcel {
	WritableWorkbook workbook = null;// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
	File file = null;

	public ExportToExcel(File file) {
		if (file.exists()) {// 锟侥碱拷锟窖撅拷锟斤拷锟斤拷
			JOptionPane.showMessageDialog(null, "锟侥碱拷锟窖达拷锟斤拷");
		} else {// 锟侥碱拷锟斤拷锟斤拷锟斤拷锟斤拷
			try {
				workbook = Workbook.createWorkbook(file);
				this.file = file;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public File getFile() {
		return file;
	}

	/**
	 * 锟斤拷锟斤拷锟侥碱拷
	 */
	public void save() {
		try {
			workbook.write();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				workbook.close();
			} catch (WriteException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷
	 * 
	 * @param name
	 * @return
	 */
	public WritableSheet createSheet(String name) {
		int number = workbook.getNumberOfSheets();
		WritableSheet sheet = workbook.createSheet(name, number);// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
		return sheet;
	}

	/**
	 * 锟斤拷写锟斤拷锟?
	 * 
	 * @param table
	 * @param sheet
	 * @param rowNum
	 * @param columnNum
	 * @throws IOException
	 * @throws WriteException
	 * @throws RowsExceededException
	 */
	public void fillCell(ArrayList<ArrayList<Object>> table, WritableSheet sheet)
			throws Exception {
		for (int i = 0; i < table.size(); i++) {
			ArrayList<Object> row = table.get(i); // 一锟斤拷锟斤拷锟?
			for (int j = 0; j < row.size(); j++) {// 锟斤拷每一锟斤拷
				String str = row.get(j).toString();
				jxl.write.Label labelN = new jxl.write.Label(j, i, str);
				sheet.addCell(labelN);
			}
		}
	}

	/**
	 * 锟斤拷写锟斤拷锟斤拷
	 * 
	 * @param table
	 * @param sheet
	 * @param columnNum
	 * @throws WriteException
	 * @throws IOException
	 */
	public void fillColunmName(String[] columnName, WritableSheet sheet)
			throws Exception {
		WritableFont font = new WritableFont(WritableFont.ARIAL, 12,
				WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
				Colour.BLACK);// 锟斤拷锟斤拷锟斤拷锟斤拷
		WritableCellFormat format = new WritableCellFormat(font);// 锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟?
		format.setAlignment(Alignment.CENTRE);// 水平锟斤拷锟斤拷锟斤拷示
		for (int k = 0; k < columnName.length; k++) {
			jxl.write.Label labelN = new jxl.write.Label(k, 0, columnName[k],
					format);
			sheet.setColumnView(0, 15);// 锟斤拷锟斤拷锟叫匡拷
			sheet.addCell(labelN);
		}
	}

}
